﻿using System;
using Engine.Core.Interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Engine.Particles.Effects
{
    public class Particle : ParticleBase, IPhysicElement
    {
        private SpriteBatch _spriteBatch;
        private Rectangle _sourceRectangle;
        private Vector2 _origin;
        public Texture2D Texture { get; set; }
        public Vector2 Position { get; set; }
        public Vector2 Acceleration { get; set; }

        public int Lifetime { get; set; }

        public Color Color { get; set; }
        public float Size { get; set; }
        public int TimeToLive { get; set; }

        public static Particle Create(Texture2D texture, Vector2 position, Color color, float size, int timeToLive, Vector2 acceleration )
        {
            return new Particle()
                       {
                           Texture = texture,
                           Position = position,
                           Color = color,
                           Size = size,
                           TimeToLive = timeToLive,
                           Acceleration = acceleration,
                       };
        }

        public Particle() {
            RenderAction = batch => batch.Draw(Texture, Position, _sourceRectangle, Color, 0, _origin, Size, SpriteEffects.None, 0f);
        }

        public virtual void Update(GameTime gameTime) {
            TimeToLive--;
            Lifetime++;
        }

        

        public override void Draw(GameTime gameTime){
            _sourceRectangle = new Rectangle(0, 0, Texture.Width, Texture.Height);
            _origin = new Vector2(Texture.Width / 2, Texture.Height / 2);
            
        }

        public override void SetSpriteBatch(SpriteBatch spriteBatch){
            if (_spriteBatch == null)
                _spriteBatch = spriteBatch;
        }

        public override sealed Action<SpriteBatch> RenderAction { get; set; }
       
    }
}
